﻿using MySql.Data.MySqlClient;
using PersistentObject.Common;
using PersistentObject.Common.Schema;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PersistentObject.MySql
{
    public class MySqlDialect : BaseDialect
    {
        public MySqlDialect(SchemaConfig config = null)
            : base(config)
        {

        }

        protected override void InitializeNamingPattern()
        {
            NamingPattern = new DialectNamingPatternForMySql();
        }

        public override System.Data.IDbConnection CreateConnection(string connectionString)
        {
            return new MySqlConnection(connectionString);
        }

        public override BaseCriteriaCreator GetCriteriaCreator(Common.Schema.Table mainTable)
        {
            return new CriteriaCreatorForMySql(mainTable, this);
        }

        public override System.Data.IDbTransaction CreateTransaction(System.Data.IDbConnection connection)
        {
            MySqlConnection sqlConnection = (connection as MySqlConnection);
            bool wasClosed = sqlConnection != null && sqlConnection.State == ConnectionState.Closed;
            if (wasClosed)
            {
                sqlConnection.Open();
            }
            return sqlConnection.BeginTransaction();
        }

        public override System.Data.IDbCommand CreateCommand(System.Data.IDbTransaction transaction, string sql)
        {
            MySqlConnection sqlConnection = (transaction.Connection as MySqlConnection);
            MySqlTransaction sqlTransaction = (transaction as MySqlTransaction);
            if (transaction == null)
            {
                MySqlCommand command = new MySqlCommand(sql, sqlConnection);
                command.CommandTimeout = 300;
                return command;
            }
            else
            {
                MySqlCommand command = new MySqlCommand(sql, sqlConnection, sqlTransaction);
                command.CommandTimeout = 300;
                return command;
            }
        }

        public override System.Data.IDbCommand CreateCommand(System.Data.IDbConnection connection, string sql)
        {
            MySqlConnection sqlConnection = (connection as MySqlConnection);
            return new MySqlCommand(sql, sqlConnection);
        }

        public override System.Data.IDbDataAdapter CreateAdapter(System.Data.IDbConnection connection, string sql)
        {
            MySqlConnection sqlConnection = (connection as MySqlConnection);
            return new MySqlDataAdapter(sql, sqlConnection);
        }

        public override System.Data.IDbDataParameter CreateParameter()
        {
            return new MySqlParameter();
        }

        public override BaseSqlCreator GetSqlCreator(Common.Schema.Table mainTable)
        {
            return new SqlCreatorForMySql(mainTable, this);
        }

        public override object RetrieveKeyValue<T>(IDataReader reader, IDbCommand command)
        {
            if (reader.Read())
            {
                var keyValue = reader.GetValue(0);
                return keyValue;
            }
            else
            {
                return null;
            }
        }
    }
}
